import pandas as pd
import os
import time
import sys
import re
import numpy as np
import glob
from QTBasicFun.week2 import *



# reference 
###  1、 https://blog.csdn.net/brucewong0516/article/details/84768464 详解pandas.DataFrame.resample根据时间聚合采样
###  2、 https://blog.csdn.net/the_time_runner/article/details/86619766  Pandas.df.resample()错误TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of ‘Index’【已解决】
###  3、https://www.cnblogs.com/Cheryol/p/13508151.html 重采样（resampling）指的是将时间序列从一个频率转换到另一个频率的过程，OHLC重采样


def find_csv_files(folder_path):
    path_name = f"{folder_path}/*.csv"
    file_list = []
    for csv_file in glob.glob(path_name):
        file_list.append(csv_file)
    return file_list


if __name__ == '__main__':
    InputDir = (sys.argv)[1]
    OutputDir = (sys.argv)[2]
    ProcessFiles = find_csv_files(InputDir)
    for OneProcessFile in ProcessFiles:
        (filepath,filenamewithext) = os.path.split(OneProcessFile)
        (filename,extension) = os.path.splitext(filenamewithext)
        symbols = re.findall("[a-z]+", filename)
        symbol = symbols[0]
        dfread = pd.read_csv(OneProcessFile, encoding='gb2312')
        
        dfread.columns = ['trade_date', 'symbol', 'exchange', 'symbol_in_exchange', 'last_price', 'prev_settle', 'prev_close', 'prev_open_interest',
                  'open', 'high', 'low', 'volume', 'turnover', 'open_interest', 'close', 'settle', 'limit_up',
                  'limit_down', 'prev_otm', 'otm', 'time', 'millisecond', 'bid_price1', 'bid_volume1', 'ask_price1', 'ask_volume1',
                  'bid_price2', 'bid_volume2', 'ask_price2', 'ask_volume2', 'bid_price3', 'bid_volume4', 'ask_price3', 'ask_volume3',
                  'bid_price4', 'bid_volume4', 'ask_price4', 'ask_volume4', 'bid_price5', 'bid_volume5', 'ask_price5', 'ask_volume5',
                  'avg_price', 'date']

        dfread['avg_price2'] = dfread.turnover / dfread.volume

        COLS_TO_DROP = ['exchange', 'symbol_in_exchange', 'bid_price2', 'bid_volume2', 'ask_price2', 'ask_volume2',
                    'bid_price3', 'bid_volume4', 'ask_price3', 'ask_volume3', 'bid_price4', 'bid_volume4',
                    'ask_price4', 'ask_volume4', 'bid_price5', 'bid_volume5', 'ask_price5', 'ask_volume5',
                    'prev_otm', 'otm', 'close', 'settle']
        dfread = dfread.drop(COLS_TO_DROP, axis=1)
        dfremain,dfout = raw2tick(dfread, symbol)
        print("丢弃的数据如下：")
        print(dfout)
        
        df1min = tick2min(dfremain)

        if not os.path.exists(OutputDir):
            os.mkdir(OutputDir)

        df1min.to_csv(OutputDir+'/'+filename + '1min'+extension,index = None)




    
    print(ProcessFile)
